let house_id = 0
let house_index = 0
Page({
  data: {
    dialogVisible: false,
    houseList: []
  },
  // onLoad() {
  //   wx.http.get('/room')
  // },
  onShow() {
    this.getHouseList()
  },
  // 过去房屋列表
  async getHouseList() {
    const { code, data } = await wx.http.get('/room')
    console.log(data);
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList: data
    })
  },
  // 点击侧滑组件中的删除按钮
  clickDeleteBtn(ev) {
    // console.log('点击了侧滑组件中的删除按钮')
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      house_id = ev.mark.id
      house_index = ev.mark.index

      // 关闭 swiper-cell 滑块组件
      instance.close()
    }
  },
  // 确认框关闭
  async dialogClose(ev) {
    // console.log('确认框关闭了')
    console.log(ev.detail)
    if (ev.detail === 'confirm') {
      const { code } = await wx.http.delete('/room/' + house_id)
      if (code !== 10000) return wx.utils.toast()

      // 更新房屋列表
      this.data.houseList.splice(house_index, 1)
      this.setData({
        houseList: this.data.houseList
      })
    }
  },

  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
